import modelExtend from 'dva-model-extend'
import { lwjPageModel } from 'utils/model'
import { routerRedux } from 'dva/router'
import { Modal, message } from 'antd'
import { config } from 'utils'
import { queryRecommendProduct, query, doRecommend, delRecommend, updateRecommend } from './services/product'

export default modelExtend(lwjPageModel, {
  //命名空间
  namespace: 'recommendlist',
  //数据
  state: {
    list: [],
    queryParam: {},
    selectedRows: [],
    selectedRowKeys: [],
    products: {
      modalVisible: false,
      selectedRows: [],
      selectedRowKeys: [],
      list: [],
      pagination: {
        current: 1,
        total: 0,
        showTotal: total => `共 ${total} 条`,
        showSizeChanger: true,
        showQuickJumper: true,
        defaultPageSize: 10,
        pageSize: 10
      },
      queryParam: {}
    }
  },
  subscriptions: {
    //页面初始化事件
    setup({dispatch, history}) {
      history.listen(({ pathname }) => {
        if (pathname === '/product/recommend') {
          dispatch({
            type: 'reset'
          })
          dispatch({ type: 'query'})
        }
      })
    }
  },
  effects: {
    //请求异步数据
    * query({}, { select, call, put }) {
      const queryParam = yield select(({ recommendlist }) => recommendlist.queryParam);
      //调用异步service
      const {result} = yield call(queryRecommendProduct, queryParam);
      //调用同步事件
      yield put({
        type: 'success',
        payload: {
          list: result
        },
      });
    },
    * productList({ payload }, { select, call, put }) {
      const products = yield select(({ recommendlist }) => recommendlist.products);
      const { result: data } = yield call(query, {
        start: payload.start,
        limit: payload.limit,
        sort: 'id',
        order: 'DESC',
        ...products.queryParam
      });
      //调用同步事件
      yield put({
        type: 'productSuccess',
        payload: {
          list: data.content,
          pagination: {
            current: payload.start + 1,
            total: data.totalElements,
            showTotal: total => `共 ${total} 条`,
            showSizeChanger: true,
            showQuickJumper: true,
            defaultPageSize: 10,
            pageSize: data.size
          }
        }
      });
    },
    * doRecommend({ params }, { select, call, put }) {
      const { result: data } = yield call(doRecommend, params);
      message.success("操作成功");
      yield put({
        type: 'setSelectedProductRows',
        selectedRows: [], 
        selectedRowKeys: []
      })
      yield put({
        type: 'query'
      });
    },
    * delRecommend({ params }, { select, call, put }) {
      const { result: data } = yield call(delRecommend, params);
      message.success("操作成功");
      yield put({
        type: 'setSelectedRowKeys',
        selectedRows: [], 
        selectedRowKeys: []
      })
      yield put({
        type: 'query'
      });
    },
    * updateRecommend({ params }, { select, call, put }) {
      const { result: data } = yield call(updateRecommend, params);
      message.success("操作成功");
    },
  },
  reducers: {
    //同步state数据
    success(state, {payload}) {
      return {
        ...state,
        list: payload.list
      }
    },
    //同步state数据
    changeFilter(state, {queryParam}) {
      return {
        ...state,
          queryParam
      }
    },
    productSuccess(state, { payload }) {
      let products = state.products;
      products.pagination = payload.pagination;
      products.list = payload.list;
      return {
        ...state,
        products
      }
    },
    setSelectedRowKeys(state, { selectedRows, selectedRowKeys }){
      return {
        ...state,
        selectedRows,
        selectedRowKeys
      }
    },
    toggleModal(state, { flag}) {
      return {
        ...state,
        products: {
          modalVisible: flag,
          selectedRowKeys: [],
          selectedRows: [],
          list: [],
          pagination: {
            current: 1,
            total: 0,
            showTotal: total => `共 ${total} 条`,
            showSizeChanger: true,
            showQuickJumper: true,
            defaultPageSize: 10,
            pageSize: 10
          },
          queryParam: {}
        }
      }
    },
    changeParam(state, { queryParam }) {
      let products = state.products;
      products.queryParam = queryParam;
      return {
        ...state,
        products
      }
    },
    setSelectedProductRows(state, { selectedRows, selectedRowKeys }) {
      let products = state.products;
      products.selectedRows = selectedRows;
      products.selectedRowKeys = selectedRowKeys;
      return {
        ...state,
        products
      }
    },
    reset(){
      return{
        list: [],
        queryParam: {},
        selectedRows: [],
        selectedRowKeys: [],
        products: {
          modalVisible: false,
          selectedRows: [],
          selectedRowKeys: [],
          list: [],
          pagination: {
            current: 1,
            total: 0,
            showTotal: total => `共 ${total} 条`,
            showSizeChanger: true,
            showQuickJumper: true,
            defaultPageSize: 10,
            pageSize: 10
          },
          queryParam: {}
        }
      }
    }
  }
})
